home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / jam / jamdisk3 / screensave / screensave.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  10KB  |  382 lines

  1. /*
  2.  *  ScreenSave.c --  v1.06 Carolyn Scheppner  CBM
  3.  *                   Saves front screen as ILBM file
  4.  *                   Saves a CAMG chunk for HAM, etc.
  5.  *                   Creates icon for ILBM file
  6.  *  Original 10/86
  7.  *  Modified 9/88 - To mask out unwanted ViewMode bits in CAMG
  8.  *                  and use CAMG defs in new ilbm.h
  9.  *
  10.  *     Uses IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  11.  *
  12.  *       (all C code including IFF modules compiled with -v on LC2)
  13.  * Linkage information:
  14.  * FROM     AStartup.obj, ScreenSave.o, iffw.o, ilbmw.o, packer.o
  15.  * TO       ScreenSave
  16.  * LIBRARY  Amiga.lib, LC.lib
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <libraries/dos.h>
  22. #include <libraries/dosextens.h>
  23. #include <graphics/gfxbase.h>
  24. #include <graphics/rastport.h>
  25. #include <graphics/gfx.h>
  26. #include <graphics/view.h>
  27. #include <intuition/intuition.h>
  28. #include <intuition/intuitionbase.h>
  29. #include <workbench/workbench.h>
  30. #include <workbench/startup.h>
  31. #include "iff/ilbm.h"
  32.  
  33. /* From AStartup - used to create stdio on WB startup */
  34. extern  LONG  stdin, stdout, stderr;
  35.  
  36. /* For masking unwanted Viewmodes bits */
  37. #define BADFLAGS  (SPRITES|VP_HIDE|GENLOCK_AUDIO|GENLOCK_VIDEO)
  38. #define FLAGMASK  (~BADFLAGS)
  39. #define CAMGMASK  (FLAGMASK & 0x0000FFFFL)
  40.  
  41. /* Other Stuff */
  42.  
  43. #define bufSize 512
  44.  
  45. struct IntuitionBase *IntuitionBase;
  46. struct GfxBase       *GfxBase;
  47. ULONG  IconBase;
  48.  
  49. struct Screen   *frontScreen;
  50.  
  51. struct ViewPort *picViewPort;
  52. struct BitMap   *picBitMap;
  53. WORD            *picColorTable;
  54. ULONG            picViewModes;
  55. BOOL fromWB, newStdio;
  56.  
  57. #define INBUFSZ 40
  58. char sbuf[INBUFSZ];
  59. char nbuf[INBUFSZ];
  60.  
  61. char conSpec[] = "CON:0/40/639/160/ ScreenSave v1.06 ";
  62.  
  63. /* Definitions for ILBM Icon */
  64. USHORT  ILBMimagedata[] = {
  65.  0xFFFF, 0xFFFC,
  66.  0xC000, 0x000C,
  67.  0xC000, 0x000C,
  68.  0xC1E7, 0x9E0C,
  69.  0xC1F8, 0x7E0C,
  70.  0xC078, 0x780C,
  71.  0xC187, 0x860C,
  72.  0xC078, 0x780C,
  73.  0xC1F8, 0x7E0C,
  74.  0xC1E7, 0x9E0C,
  75.  0xC000, 0x000C,
  76.  0xC000, 0x000C,
  77.  0xFFFF, 0xFFFC,
  78.  0x0000, 0x0000,
  79.  0x0000, 0x0000,
  80. /**/
  81.  0xFFFF, 0xFFFC,
  82.  0xFFFF, 0xFFFC,
  83.  0xF800, 0x007C,
  84.  0xF9E0, 0x1E7C,
  85.  0xF980, 0x067C,
  86.  0xF807, 0x807C,
  87.  0xF81F, 0xE07C,
  88.  0xF807, 0x807C,
  89.  0xF980, 0x067C,
  90.  0xF9E0, 0x1E7C,
  91.  0xF800, 0x007C,
  92.  0xFFFF, 0xFFFC,
  93.  0xFFFF, 0xFFFC,
  94.  0x0000, 0x0000,
  95.  0x0000, 0x0000,
  96. /**/
  97.  };
  98.  
  99. struct Image ILBMimage = {
  100.    0,0,                     /* Leftedge, Topedge */
  101.    30,15,                   /* Width Height */
  102.    2,                       /* Depth */
  103.    &ILBMimagedata[0],       /* Data for image */
  104.    3,0                      /* PlanePick, PlaneOnOff */
  105.    };
  106.  
  107. struct DiskObject ILBMobject = {
  108.    WB_DISKMAGIC,
  109.    WB_DISKVERSION,
  110.  
  111.    /* Gadget Structure */
  112.    NULL,                    /* Ptr to next gadget */
  113.    0,0,                     /* Leftedge, Topedge */
  114.    30,15,                   /* Width, Height */
  115.    GADGHBOX|GADGIMAGE,      /* Flags */
  116.    RELVERIFY|GADGIMMEDIATE, /* Activation */
  117.    BOOLGADGET,              /* Type */
  118.    (APTR)&ILBMimage,        /* Render */
  119.    NULL,                    /* Select Render */
  120.    NULL,                    /* Text */
  121.    NULL,NULL,NULL,NULL,     /* Exclude, Special, ID, UserData */
  122.  
  123.    4,                       /* WBObject type */
  124.    ":Display",              /* Default tool */
  125.    NULL,                    /* Tool Types */
  126.    NO_ICON_POSITION,        /* Current X */
  127.    NO_ICON_POSITION,        /* Current Y */
  128.    NULL,NULL,NULL,          /* Drawer, ToolWindow, Stack */
  129.    };
  130.  
  131. main(argc, argv)
  132. int argc;
  133. char **argv;
  134.    {
  135.    LONG            file;
  136.    IFFP            iffp = NO_FILE;
  137.    char            *filename;
  138.    int l;
  139.  
  140.    newStdio = FALSE;
  141.    fromWB = (argc==0) ? TRUE : FALSE;
  142.  
  143.    if((fromWB) && (!(newStdio = openStdio(&conSpec[0]))))
  144.       {
  145.       return(0);
  146.       }
  147.  
  148.    if ((IntuitionBase =
  149.       (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  150.          cleanexit("Can't open intuition.library\n");
  151.  
  152.    if ((GfxBase =
  153.       (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  154.       cleanexit("Can't open graphics.library\n");
  155.  
  156.    if ((IconBase = OpenLibrary("icon.library",0))==NULL )
  157.       cleanexit("Can't open icon.library\n");
  158.  
  159.    printf("ScreenSave v 1.06 --- C. Scheppner  CBM  9/88\n");
  160.    printf("   Saves the front screen as an IFF ILBM file\n");
  161.    printf("   A CAMG chunk is saved (for HAM pics, etc.)\n\n");
  162.  
  163.    if(argc>1)                 /* Passed filename via command line  */
  164.       {
  165.       filename = argv[1];
  166.       }
  167.    else
  168.       {
  169.       printf("Enter filename for save: ");
  170.       l = gets(&nbuf[0]);
  171.  
  172.       if(l==0)                /* No filename - Exit */
  173.          {
  174.          cleanexit("\nScreen not saved, filename required\n");
  175.          }
  176.       else
  177.          {
  178.          filename = &nbuf[0];
  179.          }
  180.       }
  181.  
  182.    if (!(file = Open(filename, MODE_NEWFILE)))
  183.       cleanexit("Can't open output file\n");
  184.      
  185.    Write(file,"x",1);  /* 1.1 so Seek to beginning works ? */
  186.  
  187.    printf("Click here and press <RETURN> when ready: ");
  188.    gets(&sbuf[0]);
  189.    printf("Front screen will be saved in 10 seconds\n");
  190.    Delay(500);
  191.  
  192.    Forbid();
  193.    frontScreen  = IntuitionBase->FirstScreen;
  194.    Permit();
  195.  
  196.    picViewPort = &( frontScreen->ViewPort );
  197.    picBitMap = (struct BitMap*)picViewPort->RasInfo->BitMap;
  198.    picColorTable = (WORD *)picViewPort->ColorMap->ColorTable;
  199.    picViewModes = (ULONG)picViewPort->Modes;
  200.  
  201.    printf("\nSaving...\n");
  202.  
  203.    iffp = PutPicture(file, picBitMap, picColorTable, picViewModes);
  204.    Close(file);
  205.  
  206.    if (iffp == IFF_OKAY)
  207.       {
  208.       printf("Screen saved\n");
  209.       if(!(PutDiskObject(filename,&ILBMobject)))
  210.          {
  211.          cleanexit("Error saving icon\n");
  212.          }
  213.       printf("Icon saved\n");
  214.       }
  215.    cleanexit("Done\n");
  216.    }
  217.  
  218. cleanexit(s)
  219.    char  *s;
  220.    {
  221.    if(*s) printf(s);
  222.    if ((fromWB)&&(*s))    /* Wait so user can read messages */
  223.       {
  224.       printf("\nPRESS RETURN TO EXIT\n");
  225.       gets(&sbuf[0]);
  226.       }
  227.    cleanup();
  228.    exit();
  229.    }
  230.  
  231. cleanup()
  232.    {
  233.    if (newStdio)  closeStdio();
  234.    if (GfxBase) CloseLibrary(GfxBase);
  235.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  236.    if (IconBase) CloseLibrary(IconBase);
  237.    }
  238.  
  239. openStdio(conspec)
  240. char *conspec;
  241.    {
  242.    LONG wfile;
  243.    struct Process *proc;
  244.    struct FileHandle *handle;
  245.  
  246.    if (!(wfile = Open(conspec,MODE_NEWFILE)))  return(0);
  247.    stdin  = wfile;
  248.    stdout = wfile;
  249.    stderr = wfile;
  250.    handle = (struct FileHandle *)(wfile << 2);
  251.    proc = (struct Process *)FindTask(NULL);
  252.    proc->pr_ConsoleTask = (APTR)(handle->fh_Type);
  253.    proc->pr_CIS = (BPTR)stdin;
  254.    proc->pr_COS = (BPTR)stdout;
  255.    return(1);
  256.    }
  257.  
  258. closeStdio()
  259.    {
  260.    struct Process *proc;
  261.    struct FileHandle *handle;
  262.  
  263.    if (stdin > 0)  Close(stdin);
  264.    stdin  = -1;
  265.    stdout = -1;
  266.    stderr = -1;
  267.    handle = (struct FileHandle *)(stdin << 2);
  268.    proc = (struct Process *)FindTask(NULL);
  269.    proc->pr_ConsoleTask = NULL;
  270.    proc->pr_CIS = NULL;
  271.    proc->pr_COS = NULL;
  272.    }
  273.  
  274. gets(s)
  275. char *s;
  276.    {
  277.    int l = 0, max = INBUFSZ - 1;
  278.  
  279.    while (((*s = getchar()) !='\n' )&&(l < max)) s++, l++;
  280.    *s = NULL;
  281.    return(l);
  282.    }
  283.  
  284. /* String Functions */
  285.  
  286. strlen(s)
  287. char *s;
  288.    {
  289.    int i = 0;
  290.    while(*s++) i++;
  291.    return(i);
  292.    }
  293.  
  294. strcpy(to,from)
  295. char *to, *from;
  296.    {
  297.    do
  298.       {
  299.       *to++ = *from;
  300.       }
  301.    while(*from++);
  302.    }
  303.  
  304. /* PutPicture()
  305.  *
  306.  * Put a picture into an IFF file.
  307.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  308.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  309.  * write out all the bitplanes in the BitMap.
  310.  *
  311.  */
  312. Point2D nullPoint = {0, 0};
  313.  
  314. IFFP PutPicture(file, bitmap, colorMap, viewmodes)
  315.       LONG file;  struct BitMap *bitmap;
  316.       WORD *colorMap;  ULONG viewmodes;
  317.    {
  318.    BYTE buffer[bufSize];
  319.    return( PutAnILBM(file, bitmap, NULL,
  320.            colorMap, bitmap->Depth, viewmodes,
  321.            &nullPoint, buffer, bufSize) );
  322.    }    
  323.  
  324. /* PutAnILBM()
  325.  *
  326.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  327.  * This version works for any display mode (C. Scheppner).
  328.  *
  329.  * Normal return result is IFF_OKAY.
  330.  *
  331.  * The utility program IFFCheck would print the following outline of the
  332.  * resulting file:
  333.  *
  334.  *   FORM ILBM
  335.  *     BMHD
  336.  *     CAMG
  337.  *     CMAP
  338.  *     BODY       (compressed)
  339.  *
  340.  */
  341. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  342.  
  343. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth,
  344.                                 viewmodes, xy, buffer, bufsize)
  345.       LONG file;
  346.       struct BitMap *bitmap;
  347.       BYTE *mask;  WORD *colorMap; UBYTE depth;
  348.       ULONG viewmodes;
  349.       Point2D *xy; BYTE *buffer;  LONG bufsize;
  350.    {
  351.    BitMapHeader bmHdr;
  352.    CamgChunk    camgChunk;
  353.    GroupContext fileContext, formContext;
  354.    IFFP ifferr;
  355.    WORD pageWidth, pageHeight;
  356.  
  357.    pageWidth  = (bitmap->BytesPerRow) << 3;
  358.    pageHeight = bitmap->Rows;
  359.  
  360.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  361.                       cmpByteRun1, 0, pageWidth, pageHeight);
  362.    /* You could write an uncompressed image by passing cmpNone instead
  363.     * of cmpByteRun1 to InitBMHdr. */
  364.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  365.    if (mask != NULL) bmHdr.masking = mskHasMask;
  366.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  367.  
  368.    camgChunk.ViewModes = viewmodes & CAMGMASK; /* Mask out unwanted bits! */
  369.  
  370.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  371.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  372.  
  373.    CkErr( PutBMHD(&formContext, &bmHdr) );
  374.    CkErr( PutCAMG(&formContext, &camgChunk) );
  375.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  376.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  377.  
  378.    CkErr( EndWGroup(&formContext) );
  379.    CkErr( CloseWGroup(&fileContext) );
  380.    return( ifferr );
  381.    }
  382.